package com.xzz.finger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: hhz
 * @create: 2022-02-17 14:07
 * 剑指 Offer 06. 从尾到头打印链表
 **/
public class ReversePrint {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(3);
        ListNode l3 = new ListNode(2);
        l1.next = l2;
        l2.next = l3;
        System.out.println(Arrays.toString(reversePrint(l1)));
    }
    static List<Integer> arr;
    public static int[] reversePrint(ListNode head) {
        arr = new ArrayList<>();
        recur(head);
        int[] res = new int[arr.size()];
        int i = 0;
        for (Integer integer : arr) {
            res[i++] = integer;
        }
        return res;
    }

    public static void recur(ListNode head) {
        if(head == null){
            return;
        }
        recur(head.next);
        arr.add(head.val);
    }
}
